from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pyautogui  # pip install opencv-python pyautogui
import pickle
import _thread
from tqdm import tqdm
from com import *



def probar(alltime):
    for i in tqdm(range(1, (alltime*60+90))):
        time.sleep(1)

def autoLogin():
    try:
        cookies = pickle.load(open(str(root/"cookies.tmp"), "rb"))
        for cookie in cookies:
            browser.add_cookie(cookie)
            log.debug(cookie['value'])
    except  Exception as e:
        login("15012349791","sy202210")
        # log.info(getcodes('//*[@id="imgVerifity"]'))
        # //*[@id="txtUserAccount"]
        # //*[@id="txtPassword"]
        # //*[@id="txtcode"]
        # //*[@id="imgVerifity"]
        # //*[@id="aLogin"]
    browser.get("https://xuexi.chinabett.com/")
    savecookies()
def login(username,pwd):
    if(not waitElement("//*[@id=\"aLogin\"]")):
        return False

    browser.find_element(By.XPATH,'//*[@id="txtUserAccount"]').send_keys(username)
    pw=browser.find_element(By.XPATH,'//*[@id="txtPassword"]').send_keys(pwd)
    ActionChains(browser).send_keys(Keys.PAGE_DOWN).perform()
    logb=browser.find_element(By.XPATH,'//*[@id="aLogin"]')
    
    # WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,'//*[@id="popLogin"]/div/form/div[5]/button')))
    #//*[@id="layui-layer15"]/div  验证码错误
    # actions = ActionChains(driver)
    # actions.move_to_element(logb)
    # actions.click(logb)
    # actions.perform()
    
    for i in range(5):
        codes=getcodes('//*[@id="imgVerifity"]')
        browser.find_element(By.XPATH,'//*[@id="txtcode"]').clear()
        browser.find_element(By.XPATH,'//*[@id="txtcode"]').send_keys(codes)
        time.sleep(0.2)
        logb.click()
        time.sleep(2)
        if(waitElement('//*[@id="imgVerifity"]',1)):
            log.info("验证码错误！")
            time.sleep(1)
            # chaojiying.ReportError(codes['pic_id'])
        else:
            log.info(f"{username}登录成功{codes}")
            return True
            break
    return False


def savecookies():
    global browser
    pickle.dump(browser.get_cookies(), open("cookies.tmp","wb"))
def clickExit():
    r_location = pyautogui.locateOnScreen(str(root/"01exit.png"), confidence=0.8, grayscale=True)
    if(r_location!=None):
        mouse_x=r_location[0]+8
        mouse_y=r_location[1]+10
        pyautogui.moveTo(mouse_x,mouse_y)
        pyautogui.click()
def playlist(list):
    for i in list:
        print(i[0],i[1])
        browser.get(i[1])
        time.sleep(2)
        clickExit()
        time.sleep(2)
        playbutton=findxpath("//*[@id=\"replaybtn\"]")
        clickobj(playbutton)
        # savecookies()
        probar(i[0])

if __name__ =="__main__":
    list=[
        # （云南）初中优秀课题展示：功的教学设计优化（一线课例分享）
        # [10,'https://video.edueva.org/studyduration/index?cid=3a32a2a0bc4549abbb59af5800cd29e1&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=82a190a931634c84b183af5800cd2a1e&t=24'],
        # [27,'https://video.edueva.org/studyduration/index?cid=3a32a2a0bc4549abbb59af5800cd29e1&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=27e38ef56c674d91add5af5800cd2a51&t=24'],
        # [15,'https://video.edueva.org/studyduration/index?cid=3a32a2a0bc4549abbb59af5800cd29e1&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=e3001b0108204e88a7f9af5800cd2a89&t=24'],

        
# （云南）实验9.测量固体和液体的密度（实验操作能力提升）
        # [23,'https://video.edueva.org/studyduration/index?cid=461bebebb4a24c9ca598af5100f57dc5&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=a8a524e5e6f1424dbb4aaf5100f57e02&t=24'],

# （云南）实验4.用常见温度计测量温度（实验操作能力提升）
        # [22,'https://video.edueva.org/studyduration/index?cid=6dfef32a0b2a475a9574af5100f58615&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=60d6f7a362cd42da9305af5100f58652&t=24'],

        
# （云南）实验12.探究水沸腾时温度变化的特点（实验操作能力提升）
        # [23,'https://video.edueva.org/studyduration/index?cid=b986d10d3067427fbe31af5100f58ed5&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=8042673f9b3c4a56a100af5100f58f1c&t=24'],

        

















# （云南）基于提升素养的物理实验创新（实验教学能力提升）
        [15,'https://video.edueva.org/studyduration/index?cid=45f1b568e8404779b6f8af5500e72c93&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=9339866fb68f4b20aa34af5500e72cd8&t=24'],
        [15,'https://video.edueva.org/studyduration/index?cid=45f1b568e8404779b6f8af5500e72c93&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=9dc97de969d8425984dfaf5500e72d10&t=24'],
        [15,'https://video.edueva.org/studyduration/index?cid=45f1b568e8404779b6f8af5500e72c93&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=c80727be056942668a04af5500e72d44&t=24'],
        [15,'https://video.edueva.org/studyduration/index?cid=45f1b568e8404779b6f8af5500e72c93&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=9d5358c4ce624210817eaf5500e72d7c&t=24'],
        [12,'https://video.edueva.org/studyduration/index?cid=45f1b568e8404779b6f8af5500e72c93&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=df604e7eec974cc49e12af5500e72db4&t=24'],

# （云南）立足新课程标准强化实验教学（实验教学能力提升）
        [16,'https://video.edueva.org/studyduration/index?cid=2a45262ba286423b8283af5500e7371e&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=7799697e0b8f4524a3acaf5500e73769&t=24'],
        [15,'https://video.edueva.org/studyduration/index?cid=2a45262ba286423b8283af5500e7371e&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=f16bd7dfb7c34cb18e6faf5500e737a1&t=24'],
        [17,'https://video.edueva.org/studyduration/index?cid=2a45262ba286423b8283af5500e7371e&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=6bbf333fe5d94813ba49af5500e737d5&t=24'],
        [14,'https://video.edueva.org/studyduration/index?cid=2a45262ba286423b8283af5500e7371e&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=6c6ddc5236824ce68ef5af5500e7380d&t=24'],
        [15,'https://video.edueva.org/studyduration/index?cid=2a45262ba286423b8283af5500e7371e&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=63b1e46eb45c4c6e91c4af5500e73845&t=24'],
        [15,'https://video.edueva.org/studyduration/index?cid=2a45262ba286423b8283af5500e7371e&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=6edf863fa0374ad980e0af5500e73879&t=24'],
        [13,'https://video.edueva.org/studyduration/index?cid=2a45262ba286423b8283af5500e7371e&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=dd85888be5094081b325af5500e738b1&t=24'],

# （云南）实验20.测量小灯泡的电功率（实验操作能力提升）
        [20,'https://video.edueva.org/studyduration/index?cid=33b130f2c91e4866bf37af5100f66606&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=e9ec39b362eb4beab5d7af5100f66643&t=24'],

# （云南）实验17.探究电流与电压、电阻的关系（实验操作能力提升）
        [28,'https://video.edueva.org/studyduration/index?cid=a05891c595c74190a8b1af5100f65ddb&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=9985ba2cafd84c9895d4af5100f65e2b&t=24'],

# （云南）实验16.连接简单的串联电路和并联电路（实验操作能力提升）
        # [19,'https://video.edueva.org/studyduration/index?cid=effaa08821694967ac37af5100fefbd4&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=694ef462939e48b99848af5100fefc16&t=24'],

# （云南）实验15.探究凸透镜成像的规律实验（实验操作能力提升）
        [26,'https://video.edueva.org/studyduration/index?cid=f201f437700f47a6a797af5100fef3a0&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=dfb56e264ea6445b8dbdaf5100fef41a&t=24'],

# （云南）实验11.探究杠杆平衡条件（实验操作能力提升）
        [22,'https://video.edueva.org/studyduration/index?cid=cbca94c9cf2248b1846eaf5100f650db&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=dc836071d40741818034af5100f6511c&t=24'],
# （云南）实验10.探究浮力大小与哪些因素有关（实验操作能力提升）
        [23,'https://video.edueva.org/studyduration/index?cid=5fdfe222c66c4cae8b0daf5100f64878&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=8855053892fc402aaaf6af5100f648b5&t=24'],

# （云南）实验6.用电压表测量电压（实验操作能力提升）
        [20,'https://video.edueva.org/studyduration/index?cid=38157db81d0c425c8544af5100f63f2b&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=bc65fbcf87f64ff3ad21af5100f63f76&t=24'],

# （云南）实验5.用电流表测量电流（实验操作能力提升）
        [28,'https://video.edueva.org/studyduration/index?cid=f7e0b2e7642448549245af5100f5974e&uid=8d77ad10e18942ba8b32f5c493ae2dc5&rid=3903e5c9eac949a1a56baf5100f5978b&t=24'],

        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
        # [,''],
    ]
    browser=init("https://xuexi.chinabett.com/")
    
    autoLogin()
    js = "window.open('https://xuexi.chinabett.com/');"    #新窗口打开链接  

    browser.execute_script(js)   
    browser.get("https://xuexi.chinabett.com/")
    playlist(list)

    a=input()